exports.manager = function (req, res) {

  //获取用户请求参数信息
  var bodyParams = req.routeInfo.bodyParams

  //当前节点ID
  var os_id = bodyParams.os_id

  //最终返回结果
  var resultData = {}

  //查询战略合作伙伴
  function searchStrategicCooperation(cb) {

    //初始化战略存储数据
    resultData.strategicCooperationData = [];

    var osMasterTypeStr = "";

    //根据类型进行查询条件的判断
    if(bodyParams.u_type == 0){
      osMasterTypeStr = "/or/os_master_type-1,2"
    }else{
      osMasterTypeStr = "/os_master_type/2"
    }


    //查询平台下的所有战略合作伙伴
    R.SEND_HTTP(
      req,
      res,
      {
        url: CORE_APIConfig.coreAPI.organizationStructureExtensionInfoSearch.Request_Api_Url + osMasterTypeStr + "/os_fid/" + os_id + "/os_is_delete/0",
        method: 'POST',
        data: {
          fields: `
                  os_id,
                  os_master_type,
                  os_name,
                  osei_phone,
                  osei_address,
                  osei_create_time
              `
        }
      },
      function (data) {

        resultData.strategicCooperationData = data.result.data;

        cb();

      }
    )
  }

  //查询战略的销售统计各项数据（新增，成交，销售额）
  function searchStrategicCooperationStatistical(cb) {


    //循环查询战略信息
    F.ParallelEach(resultData.strategicCooperationData, function (item, index, callback) {

      //查询客户数
      function searchCustomerCount(eachCB) {

        R.SEND_HTTP(
          req,
          res,
          {
            url: CORE_APIConfig.coreAPI.artCustomerSearch.Request_Api_Url + "/c_osid/" + item.os_id + "/c_is_delete/0",
            method: 'POST',
            data: {
              fields: 'c_id'
            }
          },
          function (data) {

            item.customerCount = data.result.data.length;

            eachCB();

          }
        )

      }

      //查询成交与销售额
      function searchBuyCountAndBuyMoney(eachCB) {

        //初始化成交总数
        var buyCount = 0

        //初始化总销售额
        var buyMoney = 0

        R.SEND_HTTP(
          req,
          res,
          {
            url: config.external_server.apiAddress + "/organization/search_art_product_order/apo_osid/" + item.os_id,
            method: 'POST',
            data: {
              fields: `apo_real_pay`
            }
          },
          function (data) {

            //计算成交总数 和 成交金额
            buyCount = data.result.data.length;

            for (var i = 0; i < data.result.data.length; i++) {
              buyMoney += parseFloat(data.result.data[i].apo_real_pay);
            }

            item.buyCount = buyCount
            item.buyMoney = buyMoney

            eachCB()

          }
        )

      }

      //查询团队人数
      function searchTeamCount(eachCB) {

        //初始化团队人数
        var teamCount = 0

        R.SEND_HTTP(
          req,
          res,
          {
            url: CORE_APIConfig.coreAPI.userSearch.Request_Api_Url + "/u_master_osid/" + item.os_id,
            method: 'POST',
            data: {
              fields: 'u_id'
            }
          },
          function (data) {

            teamCount = data.result.data.length;

            item.teamCount = teamCount;

            eachCB();

          }
        )

      }

      //查询阅读人数
      function searchReadCount(eachCB) {

        //记录总阅读数
        var readCount = 0;

        //获取当前战略下的所有人员ID
        R.SEND_HTTP(
          req,
          res,
          {
            url: CORE_APIConfig.coreAPI.userSearch.Request_Api_Url + "/u_master_osid/" + item.os_id,
            method: 'POST',
            data: {
              fields: `
                          u_id,
                          u_nickname,
                          u_read_count
                      `
            }
          },
          function (data) {

            var userArr = data.result.data;

            //计算当前战略下销售的总阅读数
            for (var i = 0; i < userArr.length; i++) {
              readCount += parseInt(userArr[i].u_read_count);
            }

            item.readCount = readCount;

            eachCB()

          }
        )

      }

      F.Serial([
        searchCustomerCount,
        searchBuyCountAndBuyMoney,
        searchTeamCount,
        searchReadCount
      ], () => {

        callback()

      })

    }, cb)

  }

  F.Serial([
    searchStrategicCooperation,
    searchStrategicCooperationStatistical
  ], () => {
    RES.successResponse(res, resultData);
  })


}